'''作业 p211 7.1 Python源文件改写.编写一个程序，读取一个Python源程序，将文件中所有除保留字外的小写字母换成大写字母'''
import keyword

s = keyword.kwlist
# 建立保留字列表

n=input("输入一个文件名:")
f = open(n, "r").readlines()
ls = []
for i in f:
    i = i.split()
    ls.append(i)
# 建立一个以每行的所有单词为元素组成的一个列表组

with open(n,"w+") as fo:
    for i in range(len(ls)):
        if f[i].isspace():
            fo.write(" " + "\n")
            for j in range(len(ls[i])):
                x = ls[i][j]
                if x not in s:
                    x = x.upper()
                else:
                    x = x.lower()
                if x == ls[i][len(ls[i]) - 1]:  # 判定是否遍历至每行的末尾
                    fo.write(x + "\n")
                else:
                    fo.write(x + " ")